package com.xjiuge.fruit.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.xjiuge.fruit.bean.User;
import com.xjiuge.fruit.common.Md5Util;
import com.xjiuge.fruit.common.Response;
import com.xjiuge.fruit.common.ResponseCodeConstant;
import com.xjiuge.fruit.common.ValidateUtil;
import com.xjiuge.fruit.mapper.UserMapper;
import com.xjiuge.fruit.service.UserService;
import com.xjiuge.fruit.service.base.BaseServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("userService")
public class UserServiceImpl 
    extends BaseServiceImpl<User,UserMapper> 
    implements UserService {

	@Resource(name="userMapper")
	private UserMapper userMapper;
	
	
	/**
	 * 登录
	 * UserServiceImpl.login<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月17日  下午3:47:15</p>
	 * @param loginName
	 * @param loginPwd
	 * @return
	 */
	@Override
	public Response login(String loginName, String loginPwd) {
		Response response = new Response();
		Integer code = ResponseCodeConstant.Common.success.getCode();
		String msg = ResponseCodeConstant.Common.success.getValue();
		
		User user = new User();
		user.setLoginName(loginName);
		List<User> list = selectUserByUser(user);
		if(ValidateUtil.isListNull(list)){
			code = ResponseCodeConstant.Common.Business_user_not_exist.getCode();
			msg = ResponseCodeConstant.Common.Business_user_not_exist.getValue();
		}else {
			user = list.get(0);
			//md5加密密码
			String pass = Md5Util.getMd5(loginPwd);
			//验证密码
			if(!user.getLoginPwd().equals(pass)){
				code = ResponseCodeConstant.Common.Business_user_password_error.getCode();
				msg = ResponseCodeConstant.Common.Business_user_password_error.getValue();
			}else {
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("user", user);
				response.setData(map);
			}
		}
		response.setCode(code);
		response.setMsg(msg);
		return response;
	}

	/**
	 * 通过用户查询用户
	 * UserServiceImpl.selectUserByUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  上午10:46:05</p>
	 * @param user
	 * @return
	 */
	@Override
	public List<User> selectUserByUser(User user) {
		return userMapper.selectUserByUser(user);
	}

	/**
	 * 查询所有用户
	 * UserServiceImpl.selectAllUser<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  上午10:46:26</p>
	 * @return
	 */
	@Override
	public List<User> selectAllUser() {
		return userMapper.selectAllUser();
	}

	/**
	 * 添加用户
	 * UserServiceImpl.save<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  下午2:56:12</p>
	 * @param user
	 */
	@Override
	@Transactional
	public void save(User user) {
		userMapper.insertSelective(user);
	}

	/**
	 * 根据登录名称查找用户
	 * UserServiceImpl.selectUserByLoginName<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月30日  下午3:58:48</p>
	 * @param loginName
	 * @return
	 */
	@Override
	public User selectUserByLoginName(String loginName) {
		return userMapper.selectUserByLoginName(loginName);
	}

	/**
	 * 批量删除用户
	 * UserServiceImpl.deleteByIds<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月31日  上午10:52:44</p>
	 * @param ids
	 */
	@Override
	@Transactional
	public void deleteByIds(String[] ids) {
		userMapper.deleteByIds(ids);
	}

	/**
	 * 编辑用户
	 * UserServiceImpl.update<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年10月31日  下午3:51:39</p>
	 * @param user
	 */
	@Override
	public void update(User user) {
		user.setLoginPwd(Md5Util.getMd5(user.getLoginPwd()));
		userMapper.updateByPrimaryKeySelective(user);
	}


}